package com.infocollect.project.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.infocollect.common.utils.ServletUtils;
import com.infocollect.framework.security.LoginUser;
import com.infocollect.framework.security.service.TokenService;
import com.infocollect.project.api.entity.*;
import com.infocollect.project.api.helper.CurrentHelp;
import com.infocollect.project.api.mapper.TappApiConfigMapper;
import com.infocollect.project.api.req.SaveApiConfigReq;
import com.infocollect.project.api.req.SaveConfigShowCelNamesReq;
import com.infocollect.project.api.service.ITappApiConfigBackService;
import com.infocollect.project.api.service.ITappApiConfigService;
import com.infocollect.project.api.service.ITappPageService;
import com.infocollect.project.api.service.ITappSqlContentService;
import com.infocollect.project.api.vo.ApiConfigVO;
import com.infocollect.project.form.vo.R;
import com.infocollect.project.api.entity.TappApiConfigBackEntity;
import com.infocollect.project.api.entity.TappApiConfigEntity;
import com.infocollect.project.api.entity.TappPageEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service("TappApiconfigService")
public class TappApiConfigServiceImpl extends ServiceImpl<TappApiConfigMapper, TappApiConfigEntity> implements ITappApiConfigService {
    @Autowired
    private ITappApiConfigBackService apiConfigBackService;
    @Autowired
    private ITappSqlContentService sqlContentService;
    @Autowired
    private ITappPageService pageService;
    @Autowired
    private TokenService tokenService;

    /**
     * 回滚指定版本
     *
     * @param callMethodCode
     * @param versions
     */
    @Override
    public void rollBack(String callMethodCode, Integer versions) {
        TappApiConfigBackEntity backEntity = apiConfigBackService.getApiConfigVersion(callMethodCode, versions);
        QueryWrapper<TappApiConfigEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("call_method_code", callMethodCode);
        baseMapper.delete(queryWrapper);
        TappApiConfigEntity entity = new TappApiConfigEntity();
        BeanUtils.copyProperties(backEntity, entity);
        baseMapper.insert(entity);
        apiConfigBackService.delTappApiConfigBack(backEntity.getFid());
    }

    /**
     * 保存Api参数方法
     *
     * @param param
     */
    @Override
    public R<String> saveApiConfigInfo(SaveApiConfigReq param) {
        QueryWrapper<TappApiConfigEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("page_id", param.getPageId()).eq("method_code", param.getMethodCode());
        TappApiConfigEntity entity = baseMapper.selectOne(queryWrapper);
        TappPageEntity pageEntity= pageService.getById(param.getPageId());
        if(pageEntity==null){
            return R.newError("找不到包信息");
        }
        LoginUser currentUserInfo = tokenService.getLoginUser(ServletUtils.getRequest());
        ///如果存在就进行备份
        if (entity != null) {
            TappApiConfigBackEntity backEntity = new TappApiConfigBackEntity();
            BeanUtils.copyProperties(entity, backEntity);
            apiConfigBackService.backApiConfig(backEntity);
            baseMapper.deleteById(entity.getFid());
        }
        Long sqlId = IdWorker.getId();
        entity = new TappApiConfigEntity();
        BeanUtils.copyProperties(param, entity);
        entity.setExecuteSqlId(sqlId);
        entity.setCallMethodCode(pageEntity.getPageName()+"."+entity.getMethodCode());
        sqlContentService.saveSqlContent(param.getSqlContent(), sqlId);
        entity.setFcreateby(currentUserInfo.getUsername());
        entity.setFcreationdate(new Date());
        entity.setFlastupdateby(currentUserInfo.getUsername());
        entity.setFlastupdatedate(new Date());
        baseMapper.insert(entity);
        return R.newOk(entity.getFid().toString());
    }

    /**
     * 根据回调方法编码获取接口配置信息
     *
     * @param callMethodCode
     * @return
     */
    @Override
    public ApiConfigVO getApiConfigByCallMethodCode(String callMethodCode) {
        ApiConfigVO apiEntity= baseMapper.getApiConfigByCallMethodCode(callMethodCode, null);
        if(apiEntity!=null){
            if(apiEntity.getMethodType()!=null) {
                apiEntity.setMethodTypeName(apiEntity.getMethodType().getDescription());
            }
            apiEntity.setApiUrl("comm/apiComm/"+callMethodCode);
        }
        return apiEntity;
    }

    /**
     * 根据回调方法编码获取接口配置信息
     * fid
     *
     * @return
     */
    @Override
    public ApiConfigVO getApiConfigById(Long fid) {
        ApiConfigVO apiEntity= baseMapper.getApiConfigByCallMethodCode(null, fid);
        if(apiEntity!=null){
            if(apiEntity.getMethodType()!=null) {
                apiEntity.setMethodTypeName(apiEntity.getMethodType().getDescription());
            }
            apiEntity.setApiUrl("comm/apiComm/"+apiEntity.getCallMethodCode());
        }
        return apiEntity;
    }

    /**
     * Api接口进行删除是复制到了Api备份表中
     * @param fid
     * @return
     */
    @Override
    public  R<String> deleteMapping(Long fid){
        TappApiConfigEntity entity = baseMapper.selectById(fid);
        if(entity==null){
            return R.newError("删除失败！未找到Api信息！");
        }
        TappApiConfigBackEntity backEntity = new TappApiConfigBackEntity();
        BeanUtils.copyProperties(entity, backEntity);
        apiConfigBackService.backApiConfig(backEntity);
        baseMapper.deleteById(entity.getFid());
        return R.newOk();
    }

    /**
     * 保存列表显示的列
     * @param param
     * @return
     */
    @Override
    public  boolean saveConfigShowCelNames(SaveConfigShowCelNamesReq param){
        QueryWrapper<TappApiConfigEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("call_method_code",param.callMethodCodeName);
        TappApiConfigEntity apiConfigEntity= baseMapper.selectOne(queryWrapper);
        if(apiConfigEntity==null)
            return false;
        apiConfigEntity.setShowCelNames(param.getShowCelNames());
        baseMapper.updateById(apiConfigEntity);
        return true;
    }
    @Override
    @Transactional()
    public void updateTestA()
    {
        TappApiConfigEntity apiConfigEntity=new TappApiConfigEntity();
        apiConfigEntity.setFid(1348831245340028930L);
        apiConfigEntity.setMethodName("aaaaaa");
        baseMapper.updateById(apiConfigEntity);
         apiConfigEntity=new TappApiConfigEntity();
        apiConfigEntity.setFid(1348831067753197569L);
        apiConfigEntity.setMethodName("sdfsdfsdddddddddddddddddddddddddddddddddddddddddddddd");
        baseMapper.updateById(apiConfigEntity);
    }
}
